package com.application.fabric.fabriccontrol.operate.serverfabric;

import com.application.fabric.fabriccontrol.fabricinit.connect.Congateway;

import com.application.fabric.fabriccontrol.operate.entityfabric.BasicAsset;
import org.hyperledger.fabric.gateway.Contract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;

@Repository
public class ServeAsset {
    /*这是查询全部资产的指令
    * 那么运行前我们可以得到合约了 所以先注入合约初始化
    * */
    @Autowired
    private Congateway Congateway;
    /*注入实体类函数
    * */

    /*初始化资产*/

    public void InitLedger() throws Exception{
        Contract contract = Congateway.initstatus();
        contract.submitTransaction("InitLedger");
        System.out.println("Submit Transaction: CreateAsset asset13");
    }

    /*这个智能合约有问题 没写*/
    public String Queryallasset() throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.evaluateTransaction("GetAllAssets");

        System.out.println("Evaluate Transaction: GetAssetsByRange, result: " + new String(result));

        return new String(result);
    }

    /*注 觉得这个设计还是有瑕疵*/
//    private BasicAsset basicAsset;
    public String Submitallasset(BasicAsset basicAsset) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        /*参数应该是动态的 所以我们这边使用动态String去接受 所以应该是
        * */
        result = contract.submitTransaction("CreateAsset", basicAsset.getId(),basicAsset.getName(),basicAsset.getCollections(),basicAsset.getOwner(),basicAsset.getAppraisedvalue());
        return new String(result);
    }
//    public void Submitallasset( ) throws Exception{
//        Contract contract = Congateway.initstatus();
//        System.out.println("Submit Transaction: CreateAsset asset13");
//        /*参数应该是动态的 所以我们这边使用动态String去接受 所以应该是
//         * */
//        System.out.println(contract);
//        contract.submitTransaction("CreateAsset", "asset14", "yellow", "5", "Tom", "1300");
//    }


    /*通过assetid获取资产 这个很重要*/
    public String Queryassetbyid(String assetId) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.evaluateTransaction("ReadAsset", assetId);
        System.out.println("result: " + new String(result));
        return new String(result);
    }

    /*判断是否存在
    * */
    public String Knowexit(String assetId) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.evaluateTransaction("AssetExists", assetId);
        System.out.println("result: " + new String(result));
        return new String(result);
    }

    /*删除资产*/
    public String Deleteasset(String assetid) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        /*注意这边可以删除多个
        * */
        result = contract.submitTransaction("DeleteAsset", assetid);
        return new String(result);
    }

    /*资产交易*/
    /*转让 Tomoko > owner Tom
    * */
    public String TransferAsset(String assetId,String buyer ) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.submitTransaction("TransferAsset", assetId, buyer);
        return new String(result);
    }

    /*富查询
    *  Rich Query with Pagination (Only supported if CouchDB is used as state database
    *  */
    public void Richquery() throws Exception{
//        result = contract.evaluateTransaction("QueryAssetsWithPagination","{\"selector\":{\"docType\":\"asset\",\"owner\":\"Tom\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}","3","");
        /*看了下 这个先不用好了*/
    }

    /*部分资产转移 感觉这个用的会比较多
    * */
    public void Partasset(String parm,String owner) throws Exception{
        Contract contract = Congateway.initstatus();
        System.out.println("Submit Transaction: TransferAssetByColor yellow assets > newOwner Michel");
        contract.submitTransaction("TransferAssetByColor", parm, owner);
        System.out.println("成功");
    }

    /*还是部分用户的富查询
    * */
    public void QueryAssetsByOwner(String owner) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.evaluateTransaction("QueryAssetsByOwner", owner);
        System.out.println("result: " + new String(result));
    }

    /*历史数据获取
    * */
    public String Gethistorydata(String assetId) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.evaluateTransaction("QueryAssetHistory", assetId);
        System.out.println("result: " + new String(result));
        return new String(result);
    }

    /*
    * 分页查询
    * */
    public void Pagingquery() throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        result = contract.evaluateTransaction("QueryAssets", "{\"selector\":{\"size\":15}}");
        System.out.println("result: " + new String(result));
    }

    /*富查询的某一种*/
    public void Richqueryone() throws Exception{
//        System.out.println("\n");
//            System.out.println("Evaluate Transaction:QueryAssets Jin Soo's assets");
//            result = contract.evaluateTransaction("QueryAssets","{\"selector\":{\"docType\":\"asset\",\"owner\":\"Jin Soo\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}");
//            System.out.println("result: " + new String(result));
    }

    /*一定范围中的查询
    * */
    public void Queryinpaging() throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus();
        System.out.println("Evaluate Transaction:GetAssetsByRangeWithPagination assets 3-5");
        result = contract.evaluateTransaction("GetAssetsByRangeWithPagination", "asset3", "asset6", "3","");
        System.out.println("result: " + new String(result));
    }

    /*owner查询*/
    public String QueryassetbyOwner(String id) throws Exception{
        byte[] result;
        Contract contract = Congateway.initstatus04();
        result = contract.evaluateTransaction("ReadAsset",id);
        System.out.println("result: " + new String(result));
        return new String(result);
    }

//    查询String
public String getQueryResultForQueryString(String owner) throws Exception{
    byte[] result;
    Contract contract = Congateway.initstatus04();
    result = contract.evaluateTransaction("QueryAssetByOwner", "public",owner);
    System.out.println("result: " + new String(result));
    return new String(result);
}

}
